package com.anbinh.doctor.fragment.lichhen;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.adapter.SearchLichhenAdapter;
import com.anbinh.doctor.model.AppointmentResultSearch;
import com.anbinh.doctor.model.AppointmentResultSearch.Data.Item;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.IRcallBak;
import com.anbinh.doctor.utils.LichType;
import com.anbinh.doctor.utils.Utils;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;

public class FragmentResultSearch extends Fragment {
	private static FragmentResultSearch fragment = null;
	private View mView;
	private CircleProgressBar loadding;
	private SearchLichhenAdapter adapter;
	private ListView mGridView;
	protected ArrayList<Item> mList;
	private Item itemSearch;
	private static String mKeySearch;
	public HashMap<String, String> listStatus=new HashMap<String, String>();
	
	public static FragmentResultSearch newInstance(String name) {
		if (fragment == null) {
			fragment = new FragmentResultSearch();
		}
		mKeySearch = name;
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
	}

	@Override
	public void onResume() {
		super.onResume();
		loadding = (CircleProgressBar) mView.findViewById(R.id.loadding);
		onLoadData(mKeySearch);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_message, null);
		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mGridView = (ListView) mView.findViewById(R.id.listView1);
		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				itemSearch = mList.get(position);
				int status;
				String tmp = itemSearch.getStatus();
				if (tmp.isEmpty()) {
					status =Utils.LICH_TRONG;// Lịch khám trống
				} else {
					status = Integer.parseInt(tmp);
				}

				if (status == LichType.LICHTAMDUNG_NUOCBIEN.getCode()) {
					Utils.showConfirmOnPause(
							itemSearch.getSchedules_pause_id(), getActivity(),
							new IRcallBak() {

								@Override
								public void onDisplay(int type, Bundle data) {
									switch (type) {
									case Utils.SHOW_PROCESS:
										loadding.setVisibility(View.VISIBLE);
										Utils.disableEnableControls(false,
												mView);

										break;

									case Utils.GONE_PROCESS:
										loadding.setVisibility(View.INVISIBLE);
										Utils.disableEnableControls(false,
												mView);

										break;
									case Utils.SUSSCESS:
										Utils.showMessage(getActivity(),
												"Bỏ tạm hoản thành công");
										break;
									}
								}
							});
					return;
				}

				Bundle bundle = new Bundle();
				if (status == LichType.LICHTRONG_MAUXANH.getCode()) {
					bundle.putString(Utils.TITLE,
							getResources().getString(R.string.tao_lich_hen));
					bundle.putInt(Utils.KEY, Utils.CREATE_APPOINTMENT);

				} else {
					bundle.putInt(Utils.KEY, Utils.APPOINTMENT_DETAILT);
					bundle.putString(Utils.TITLE,
							getResources()
									.getString(R.string.chi_tiet_lich_hen));
				}
				bundle.putInt(Utils.KEY_ID, itemSearch.getAppointment_id());
				bundle.putString(Utils.KEY_STATUS, itemSearch.getStatus());
				bundle.putInt(Utils.REQUEST_CODE, Utils.ONRESUME_LICHHEN);
				bundle.putBoolean(Utils.CUSTOMBAR, false);
				Utils.getNewIntent(getActivity(), bundle);
			}

		});

		
	}

	public Item getCurrentAssistants() {
		return itemSearch;
	}

	public void onLoadData(String name) {
		loadding.setVisibility(View.VISIBLE);
		final UserLogin user = Utils.getAccountModel(getActivity(),
				Utils.KEY_USER);
		String token = user.getData().getToken();
		ServerController.getInstance(getActivity())
				.reuquestGetListSearchLichkham(token, name,-1,
						new OnRequest<AppointmentResultSearch>() {

							@Override
							public void onSuccess(AppointmentResultSearch data) {
								if (getActivity() == null)
									return;
								loadding.setVisibility(View.INVISIBLE);
								if (data.getResponsecode() < 0)
									return;
								if (data.getData().getItems() == null)
									return;
								mList = data.getData().getItems();
								adapter = new SearchLichhenAdapter(
										getActivity(), R.layout.item_doctor,
										mList);
								mGridView.setAdapter(adapter);
							}

							@Override
							public void onError(int errorCode, String message) {
								loadding.setVisibility(View.INVISIBLE);
							}
						});

	}
	
	
}
